package com.zlj.xialiao.controller;

import com.zlj.xialiao.dao.PublishMapper;
import com.zlj.xialiao.dao.UserMapper;
import com.zlj.xialiao.entities.Publish;
import com.zlj.xialiao.entities.User;
import com.zlj.xialiao.exception.ForbiddenLoginException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 郑霖俊
 * @create 2021-05-05 21:58
 */
@Controller

public class UserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PublishMapper publishMapper;
    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());
@PostMapping ("/loginadmin")
//    @GetMapping ("/loginadmin")
    public String login(@RequestParam("username") String username,@RequestParam("password")  String password, Map<String,Object> map, HttpSession session){
        logger.info("页面中的密码："+password+"页面中的登录名："+username);
        User login = userMapper.login(username);
//    DispatcherServlet
//        logger.info(String.valueOf(login));
        if(login==null){
            map.put("msg","用户名不存在");
            return "login";
        }
        if (login.getUsername().equals(username) && login.getPassword().equals(password)){
            logger.info(String.valueOf(login));

            session.setAttribute("loginUser",login.getNickname());
            List<Publish> publish=publishMapper.selectAll();
            logger.info("UserController:publish："+publish);
            map.put("publish",publish);
//            ExceptionHandlerExceptionResolver
//            int i=10/0;
            //ResponseStatusExceptionResolver
//            throw new ForbiddenLoginException();
            return "home";
        }else {
            map.put("msg","用户名或密码错误");
            return "login";
        }
    }

    @PostMapping("/registadmin")
//    @GetMapping("/registadmin")
    public String regist(String nickname, String username, String password, Map<String,Object> map){

        User login = userMapper.login(username);
        logger.info("页面中的密码："+password+"页面中的登录名："+username+",数据库中的login："+login);
        if (login==null){
            userMapper.regist(nickname,username,password);
            map.put("msg","注册成功");
            return "login";
        }else if(login.getUsername().equals(username)){
            map.put("msg","注册名已经存在");
            return "register";
        }
        map.put("msg","注册失败再来一次");

        return "register";

    }

    @GetMapping("/logoutadmin")
    public String logout(HttpSession session){
        //1：清空session中的用户信息
        session.removeAttribute("loginUser");
        //2：注销session
        session.invalidate();
        return "redirect:login";
    }



}
